package com.pactera.madp.admin.config;

import com.pactera.madp.admin.api.entity.SysDict;
import com.pactera.madp.admin.api.entity.SysDictItem;
import com.pactera.madp.admin.service.SysDictItemService;
import com.pactera.madp.admin.service.SysDictService;
import com.pactera.madp.common.core.constant.CacheConstants;
import com.pactera.madp.common.core.util.R;
import com.pactera.madp.common.data.tenant.TenantContextHolder;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.web.context.WebServerInitializedEvent;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.event.EventListener;
import org.springframework.core.annotation.Order;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.JdkSerializationRedisSerializer;
import org.springframework.scheduling.annotation.Async;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@Configuration
@AllArgsConstructor
public class DictInitRunner {

    private final RedisTemplate redisTemplate;
    private final SysDictService sysDictService;
    private final SysDictItemService sysDictItemService;

/*
    @Async
    @Order
    @EventListener({WebServerInitializedEvent.class})
    public void initDict(){
        log.info("初化始字典数据...开始");

        //设置字典rootKey
        StringBuilder sb = new StringBuilder();
        sb.append(TenantContextHolder.getTenantId());
        sb.append(":");
        sb.append(CacheConstants.DICT_DETAILS);
        sb.append(":");
        sb.append(SysDict.class.getSimpleName());
        String rootKey = sb.toString();

        //1.清除redis中的字典类型数据
        redisTemplate.delete(rootKey);
        redisTemplate.setValueSerializer(new Jackson2JsonRedisSerializer<>(R.class));
        redisTemplate.setValueSerializer(new JdkSerializationRedisSerializer());

        //2.从数据据查询字典类型并缓存到redis中
        List<SysDict> sysDictList = sysDictService.list();
        redisTemplate.opsForValue().set(rootKey, R.ok(sysDictList));

        //3.清除redis中字典项数据
        sysDictList.forEach(sysDict -> {
            redisTemplate.delete(redisTemplate.keys(rootKey + ":" + sysDict.getType() + "*"));
        });

        //4.从数据库查询字典项并缓存到redis中 type->items和type：value->item
        Map<String,List<SysDictItem>> itemTypeMap = new HashMap<>();
        sysDictItemService.list().forEach(sysDictItem -> {
            if(itemTypeMap.containsKey(sysDictItem.getType())){
                List<SysDictItem> list = itemTypeMap.get(sysDictItem.getType());
                list.add(sysDictItem);
            }else{
                List<SysDictItem> list = new ArrayList<>();
                list.add(sysDictItem);
                itemTypeMap.put(sysDictItem.getType(), list);
            }

            redisTemplate.opsForValue().set(rootKey + ":" + sysDictItem.getType() + ":" + sysDictItem.getValue(), R.ok(sysDictItem));
        });

        itemTypeMap.entrySet().forEach(entry -> {
            redisTemplate.opsForValue().set(rootKey + ":" + entry.getKey(), R.ok(entry.getValue()));
        });

        log.info("初化始字典数据...结束");

    }
*/

}
